import React from 'react'
import styles from './index.less'
import { useHistory } from 'dva'
import { GetFirstImg } from '../../utils/tools'
import { E } from '../../../../constants'
import { useDispatch, useSelector } from 'dva'
import { getToken } from '../../utils/authority'
import { getLocale } from "umi";
import { Currency } from '../../utils/utils';

function Index({ obj }) {
  const history = useHistory()
  const dispatch = useDispatch()
  const { userInfo } = useSelector(({ index }) => index)
  const id = 1
  // 筛选数组，保证是图片
  function filterImg(imgList) {
    const imgTypes =  'jpeg|gif|jpg|png|bmp|pic|svg|'
    let targetImg = ''
    for (const item of imgList) {
      const suffixList = item.split('.')
      const suffix = suffixList[suffixList.length - 1]
      if (imgTypes.indexOf(suffix) != -1) {
        targetImg = item
        break
      } else {
        continue
      }
    }
    return targetImg
  }

  function filterData() {
    let name = '';
    let img = '';
    let logo = '';
    let price = ''
    if (obj) {
      // console.log(obj);
      name = getLocale() == "zh-CN" ? obj.name : obj.nameEn;
      img = filterImg(obj?.properties?.albums || '');
      img = obj.properties && obj.properties?.albums && obj.properties?.albums.length>0 && obj.properties?.albums[0] ? obj.properties.albums[0] : '';
      logo = obj.exhibitor ? (obj.exhibitor.properties.logo) : '';
      price =  obj?.currency ?`${Currency[obj?.currency].description}${obj?.minimum ? obj?.minimum : ''} ~ ${Currency[obj.currency].description}${obj?.maximum ? obj?.maximum : ''}` : ''
    }
    return {
      name,
      img,
      logo,
      price
    }
  }
  let data = filterData()
  // console.log(data, 'ddddddddddddddd')
  return (
    <div className={styles.box}
      onClick={() => {
        // let token = getToken()
        // if (!token) {
        //   dispatch({
        //     type: 'index/setState',
        //     payload: {
        //       loginModalShow: true
        //     }
        //   })
        //   return
        // }

        history.push(`/index/details/${obj && obj.id}`)
      }
      }>
      <div className={styles.imgBox}>
        <img src={data.img}></img>
        <div className={styles.logoBox}>
          {
            data.logo && <img src={data.logo} className={styles.logo}></img>
          }

        </div>
      </div>
      <div className={styles.textBox}>
        <p className={styles.text}>{data.name}</p>
        {/* <p className={styles.text}>{getLocale() == "zh-CN" ? data.name : data.nameEn}</p> */}
        <p className={styles.price}>{data.price}</p>
      </div>
    </div>
  )
}

export default Index
